package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.BusinessIncomeDto;
import xyz.zhouzhaodong.crm.dto.FormDto;
import xyz.zhouzhaodong.crm.entity.BusinessIncome;
import xyz.zhouzhaodong.crm.mapper.BusinessIncomeMapper;
import xyz.zhouzhaodong.crm.service.IBusinessIncomeService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 营业收入 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-07-03
 */
@Service
public class BusinessIncomeServiceImpl extends ServiceImpl<BusinessIncomeMapper, BusinessIncome> implements IBusinessIncomeService {

    @Resource
    BusinessIncomeMapper businessIncomeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addBusinessIncome(BusinessIncome businessIncome) {
        try {
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            businessIncome.setId(id);
            businessIncome.setCreateUser(businessIncome.getStaffId());
            businessIncome.setCreateTime(new Date());
            businessIncomeMapper.insert(businessIncome);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteBusinessIncome(BusinessIncome businessIncome) {
        try {
            businessIncome.setFlag(1);
            businessIncome.setUpdateUser(businessIncome.getStaffId());
            businessIncome.setUpdateTime(new Date());
            businessIncomeMapper.updateById(businessIncome);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateBusinessIncome(BusinessIncome businessIncome) {
        try {
            businessIncome.setUpdateUser(businessIncome.getStaffId());
            businessIncome.setUpdateTime(new Date());
            businessIncome.setStaffId(null);
            businessIncomeMapper.updateById(businessIncome);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectBusinessIncome(BusinessIncomeDto businessIncomeDto) {
        Page<BusinessIncome> page = new Page<>(businessIncomeDto.getPageNo(),
                businessIncomeDto.getPageSize() == 0 ? 10 : businessIncomeDto.getPageSize());
        businessIncomeDto.setFlag(businessIncomeDto.getFlag() == null ? 0 : businessIncomeDto.getFlag());
        IPage<BusinessIncome> list = businessIncomeMapper.selectBusinessIncomeList(page, businessIncomeDto);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectCustomerForm(FormDto formDto) {
        // 日期范围 内的 营业收入  员工薪资 费用支出  费用报销   顾问
        String entry = formDto.getEntry().trim();
        String entryCheck = "营业收入";
        if(entryCheck.equals(entry)){
            formDto.setEntry("business_income");
        }
        entryCheck = "员工薪资";
        if(entryCheck.equals(entry)){
            formDto.setEntry("payroll");
        }
        entryCheck = "费用支出";
        if(entryCheck.equals(entry)){
            formDto.setEntry("expenses");
        }
        entryCheck = "费用报销";
        if(entryCheck.equals(entry)){
            formDto.setEntry("reimburse");
        }
        entryCheck = "发票";
        if(entryCheck.equals(entry)){
            formDto.setEntry("invoices_control");
        }
        List<FormDto> list = businessIncomeMapper.selectCustomerForm(formDto);
        return RestMessage.success(list);
    }
}
